package com.itcm.manage.component;

import cn.hutool.json.JSONUtil;
import com.itcm.manage.common.R;
import com.itcm.manage.common.ResultCodeEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author sky
 * @Site cmtianxie163.com 2020/6/15 16:09
 */
@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest req, HttpServletResponse resp, AuthenticationException e) throws IOException, ServletException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.getWriter().println(JSONUtil.parse(R.setResult(ResultCodeEnum.UNAUTHORIZED).data("msg",e.getMessage())));
        resp.getWriter().flush();
    }
}
